package com.hsgene.order.controller.peanut;

import com.hsgene.common.util.result.ApiResult;
import com.hsgene.order.exception.OrderErrorCode;
import com.hsgene.order.service.AgencyOrderService;
import com.hsgene.order.service.ApplyOrderService;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 检测申请单
 *
 * @author wxf
 * @date 2018/9/21 15:13
 **/
@RestController
@RequestMapping("/v1.0/genetic/testing")
public class AgencyApplyOrderController {

    /**
     * Log
     */
    private static Logger LOGGER = LogManager.getLogger(AgencyApplyOrderController.class);

    @Resource
    private ApplyOrderService applyOrderService;

    @Resource
    private AgencyOrderService agencyOrderService;

    /**
     * 申请单
     *
     * @param orderId   检测单ID
     * @param agencyIds 机构ID(多个用英文逗号隔开)
     * @return
     */
    @PostMapping("orders/{order_id}/requistions")
    public ApiResult<String> addApplyOrder(@PathVariable("order_id") String orderId, String agencyIds) {
        try {
            if (StringUtils.isEmpty(orderId)) {
                return ApiResult.fail(OrderErrorCode.ORDER_ID_IS_NOT_NULL);
            }
            String result = applyOrderService.addApplyOrder(orderId, agencyIds);
            if (result == null) {
                return new ApiResult(200, "success", "申请单生成成功", null);
            }
            return ApiResult.fail(OrderErrorCode.APPLY_ORDER_ERROR, result);
        } catch (Exception e) {
            LOGGER.error("add apply order is error...", e);
            return ApiResult.fail(OrderErrorCode.APPLY_ORDER_ERROR);
        }
    }

    /**
     * @param
     * @return com.hsgene.common.util.result.ApiResult<java.util.List<java.util.Map<java.lang.String,java.lang.Object>>>
     * @description 获取所有检测结构
     * @author maodi
     * @createDate 2018/11/1 17:16
     */
    @GetMapping("agencies")
    public ApiResult<List<Map<String, Object>>> agencies() {
        try {
            return ApiResult.succ(agencyOrderService.agencies());
        } catch (Exception e) {
            LOGGER.error(OrderErrorCode.GET_ALL_AGENCIES_IS_ERROR.getMessage(), e);
            return ApiResult.fail(OrderErrorCode.GET_ALL_AGENCIES_IS_ERROR);
        }
    }

}
